<?php
class Search extends Controller {

	function Search(){
		parent::Controller();	
	}
	function index(){
		redirect('/search/tag/');
	}
	function tag($searchTerm=NULL){
		$this->load->model('UserMahasiswa');
		$this->load->model('Posts');
		if($searchTerm==NULL){
			$data['searchTerm']='';
			$data['echoTagged'] = FALSE;
			if($this->input->post('searchTerm')){
				redirect("/search/tag/".$this->input->post('searchTerm'));
			}
		}else{
			$data['searchTerm'] = $searchTerm;
			$data['echoTagged'] = $this->Posts->searchPostsTagged($searchTerm);
			if($echoTagged = $data['echoTagged']){
				$echoTaggedMsg ="";
				for($i=0;$i<sizeOf($echoTagged['message']);$i++){
					$echoTaggedMsg = $echoTaggedMsg.$this->_formatEcho($echoTagged['user_id'][$i],$echoTagged['message'][$i],$echoTagged['date'][$i],$echoTagged['comments'][$i],$echoTagged['post_id'][$i]);	
				}
				$data['echoTaggedMsg'] = $echoTaggedMsg;
			}
		}
		if($this->session->userdata('isLogin')){
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
		}
		$data ['pageTitle'] = 'Search Tag|'.$data['searchTerm'];
		
		$this->load->view('searchTag',$data);
	}
	
	function _formatEcho($user_id,$message,$date,$commentNum=NULL,$post_id=NULL){
		
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$this->load->model('User');
		$this->load->model('Posts');
		$this->load->model('Attachment');
		$thisPost = $this->Posts->getPost($post_id);
		
		
		$attachedHTML='<ul id="listAttach">';
		if($thisPost['attached']==1){
			$postAttach = $this->Attachment->getAttachment($post_id);
			for($i=0;$i<sizeOf($postAttach['type']);$i++){
				if($postAttach['type'][$i]=='image'){
					$attachedHTML = $attachedHTML.'<li>
					<a href="'.base_url().'sknimg/i-see-attr/imageAttachment/'.$postAttach['data'][$i].'" class="lightbox-enabled" onclick="$(this).lightbox({start:true,events:false}); return false;" title="'.$postAttach['data'][$i].'">
						<img class="avatarecho" style="float:right;" src="'.base_url().'sknimg/i-see-attr/imageAttachmentThumb/'.$postAttach['data'][$i].'" width="50px" height="50px"  /></a>
					<br><strong style="font-size:11px;font-style:italic;">
					<img src="'.base_url().'sknimg/img/download.png" width="15" height="15"></img>
					<a href="'.base_url().'engine/download/item/'.$postAttach['data'][$i].'/image">
					Image Attachment: '.$postAttach['data'][$i].'</a></strong>
					<div class="tweetTxt">
					';
					$attachedHTML = $attachedHTML.'</div> <